package pso;

public class BoundaryHandlerBounce implements BoundaryHandler {

	private Point lowerBound, upperBound;
	public BoundaryHandlerBounce(Point lowerBound, Point upperBound){
		this.lowerBound = lowerBound;
		this.upperBound = upperBound;
	}
	
	@Override
	public void applyBoundaryHandling(Particle p) {
		Point pos = p.getPosition();
		Point vel = p.getVelocity();
		int dim = pos.getDimension();
		for(int i = 0; i < dim; i++){
			double x = pos.getCoordinate(i);
			double xlo = lowerBound.getCoordinate(i);
			double xhi = upperBound.getCoordinate(i);
			double velocityMul = 1.0;
			while(x < xlo || x > xhi){
				if(x < xlo){
					x = xlo * 2 - x;
				} else {
					x = xhi * 2 - x;
				}
				velocityMul = -velocityMul;
			}
			pos.setCoordinate(i, x);
			vel.setCoordinate(i, vel.getCoordinate(i) * velocityMul);
		}
	}

}
